import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: lenovo
 * Date: 2022-12-18
 * Time: 14:52
 */
@WebServlet("/userInfo")
public class userInfoServlet extends HttpServlet {
    ObjectMapper objectMapper = new ObjectMapper();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取一下query string 来判断是给blog_list还是给blog_detail返回响应
        String blogId = req.getParameter("blogId");
        if(blogId == null){
            //blog_list 获取登录用户的信息
            getUserInfoFromSession(req,resp);
        }else{
            //blog_detail 获取博客作者的信息
            getUserInfoFromDb(req,resp,Integer.parseInt(blogId));
        }

    }

    private void getUserInfoFromDb(HttpServletRequest req, HttpServletResponse resp, int blogId) throws IOException {
        //根据blogId找到博客的作者
        //1.根据blogId找到博客
        BlogDao blogDao = new BlogDao();
        Blog blog = blogDao.selectOne(blogId);
        if(blog == null){
            //blogId可能是瞎写的一个数字,在数据库里肯不存在这个blogID
            resp.setStatus(404);
            resp.setContentType("text/html ; charset=utf8");
            resp.getWriter().write("blogID不存在!!!");
            return;

        }
        //2.根据博客的userId找到作者
        int userId = blog.getUserId();
        UserDao userDao = new UserDao();
        User user = userDao.selectByUserId(userId);
        if(user == null){
            //作者不存在(理论上一篇博客应该有作者)
            resp.setStatus(404);
            resp.setContentType("text/html ; charset=utf8");
            resp.getWriter().write("作者不存在!!!");
            return;

        }

        //将user对象的password设置为空字符串
        user.setPassWord("");
        resp.setContentType("application/json ; charset=utf8");
        resp.getWriter().write(objectMapper.writeValueAsString(user));
    }

    private void getUserInfoFromSession(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取会话
        HttpSession session = req.getSession(false);
        if(session == null){//会话不存在
            resp.setStatus(403);
            resp.setContentType("text/html : charset=utf8");
            resp.getWriter().write("未登录");
            return;

        }
        User user = (User) session.getAttribute("user");
        if(user == null){
            resp.setStatus(403);
            resp.setContentType("text/html : charset=utf8");
            resp.getWriter().write("未登录");
            return;

        }
        //将user对象的password设置为空字符串
        user.setPassWord("");
        resp.setContentType("application/json ; charset=utf8");
        resp.getWriter().write(objectMapper.writeValueAsString(user));




    }
}
